<?php
/**
 * Action.php
 *
 * @category    Madoqua
 * @package     Cache
 * @subpackage  Action
 */

/** zend action */
require_once 'Zend/Cache.php';

/**
 * Madoqua_Cache_Action
 *
 * @category    Madoqua
 * @package     Cache
 * @subpackage  Action
 */
class Madoqua_Cache_Action {

    /**
     * the cache
     *
     * @var Zend_Cache_Core
     */
    protected static $_cache;

    /**
     * get the action cache
     *
     * @return Zend_Cache_Core
     */
    public static function get() {

        if (empty(self::$_cache)) {
            //cache doesn't exist yet

            $frontendOptions = array();
            $backendOptions = array(
                'cache_dir' => Madoqua_Application::getDataDirectory() . DIRECTORY_SEPARATOR  . 'cache/actions/'
                );
                //caching options

                self::$_cache = Zend_Cache::factory('core', 'file', $frontendOptions, $backendOptions);
                //the cache
        }

        return self::$_cache;
    }
    
    /**
     * clean the action cache with a specific set of tags
     *
     * @param array $tags
     */
    public static function clean($tags) 
    {
        $cache = self::get();
        //get the action cacher
        $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $tags);
        //clear all matching controller/action caches        
    }

    /**
     * does the config allow caching?
     *
     * @return bool
     */
    public static function canCache()
    {
        $config = Madoqua_Application::getConfiguration();
        return (bool) $config->caching->enable;
    }

    /**
     * is there a cached version of this request?
     *
     * @param Zend_Controller_Request_Abstract|string $request
     * @return bool
     */
    public static function hasCache($request)
    {
        return self::get()->test(self::getIdRequest($request));
    }

    /**
     * load cache for a given request
     *
     * @param Zend_Controller_Request_Abstract|string $request
     * @return bool|string
     */
    public static function loadCache($request)
    {
        return self::get()->load(self::getIdRequest($request));
    }

    /**
     * save the cache for a request
     *
     * @param Zend_Controller_Request_Abstract|string $request
     * @return bool|string
     */
    public static function saveCache($result, $request)
    {
        self::get()->save($result, self::getIdRequest($request), self::getTagsRequest($request));
    }

    /**
     * get a cache id
     *
     * @param string $module
     * @param string $controller
     * @param string $action
     * @param array $params
     * @return string
     */
    public static function getId($module, $controller, $action, $params = array())
    {
        $id = 'action_' . $module . '_' . $controller . '_' . $action;
        $id = str_replace('-', '_', $id);
        //replace any dashes that might be in controller/action/module names with underscores
        $id .= md5(serialize($params));
        //the cache id

        return $id;
    }

    /**
     * get an id for a request object
     *
     * will return string if string given
     *
     * @param Zend_Controller_Request_Abstract|string $request
     * @return string
     */
    public static function getIdRequest($request) {
        if (!is_string($request)) {
            $request = self::getId(
            $request->getModuleName(),
            $request->getControllerName(),
            $request->getActionName(),
            $request->getParams()
            );
            //cache id
        } 
        return $request;
    }

    /**
     * get an array of tags
     *
     * @param string $module
     * @param string $controller
     * @param string $action
     * @return array
     */
    public static function getTags($module, $controller, $action)
    {
        $tags = array(
            'module_' . str_replace('-', '_', $module),
            'controller_' . str_replace('-', '_', $controller),
            'action_' . str_replace('-', '_', $action)
        );
        //tags

        return $tags;
    }

    /**
     * Enter description here...
     *
     * @param Zend_Controller_Request_Abstract|string $request
     * @return string
     */
    public static function getTagsRequest($request)
    {
        if (!is_string($request)) {
            $request = self::getTags(
            $request->getModuleName(),
            $request->getControllerName(),
            $request->getActionName()
            );
            //cache id
        }
        return $request;
    }

}